﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shop.Model.Custom;
using Shop.Framework.Keys;
using Shop.Framework.Services;
using Shop.Model;
using Shop.Framework.Runtime;

namespace Shop.Website.Controllers {
    [HandleError]
    public class HomeController : Controller {

        public ActionResult Index() {
            //ViewData["Message"] = "Welcome to ASP.NET MVC!";
            HeadModel model = new HeadModel {
                Description = "Clothing",
                Tag = "Indigenous, Items, Beads",
                Keyword = "Indigenous, Items, Beads",
                Title = "Hands On"
            };
            ViewData[ShopKey.HeadModel] = model;

            return View();
        }

        
        public ActionResult GetCategories() {
            return PartialView("~/controls/CategoryBuilderControl.ascx");
        }

        public ActionResult GetDesigners() {
            return PartialView("~/controls/DesignerBuilderControl.ascx");
        }

        public int GetCurrentContext() {
             if(Session[SessionKey.CurrentContextView] == null)
                Session[SessionKey.CurrentContextView] = ContextView.Category;


             return Convert.ToInt32(Session[SessionKey.CurrentContextView]);
        }

        public ActionResult Category() {
            return View("Category");
        }

        public ActionResult Designer() {
            return View("Designers");
        }

        public ActionResult Login(string username, string password) {
            CustomerService service = new CustomerService();
            bool ok = service.Login(username, password);

            if (!ok)
                ViewData["LoginError"] = "Login error, please check your inputs..";
            else
                ViewData["LoginError"] = null;

            return RedirectToAction("Index");
        }

        public ActionResult Logout(string username, string password) {
            WebCart.Self.CurrentCustomer = null;
            return RedirectToAction("Index");
        }

        public ActionResult Register() {
            return View();
        }

        public ActionResult SaveRegistration() {

            try {

                Customer customer = new Customer {
                    Age = Convert.ToInt32(Request["Age"]),
                    BirthDate = Convert.ToDateTime(Request["BirthDate"]),
                    City = Request["City"].ToString(),
                    Country = Request["Country"].ToString(),
                    FirstName = Request["FirstName"].ToString(),
                    IdCustomer = Convert.ToInt32(Request["IdCustomer"]),
                    LastName = Request["LastName"].ToString(),
                    LoginId = Request["LoginId"].ToString(),
                    Password = Request["Password"].ToString(),
                    Street = Request["Street"].ToString(),
                    Zip = Request["Zip"].ToString()
                };

                CustomerService service = new CustomerService();
                if (customer.IdCustomer == 0) {
                    service.RegisterNew(customer);
                } else {
                    service.EditProfile(customer); 
                }

                if (Session["FromCheckout"] != null) {
                    WebCart.Self.CurrentCustomer = customer;
                    return RedirectToRoute("checkout");
                }
                CMSRuntime.Instance.SetSuccessfulRegistration();
                return RedirectToAction("Index");
            } catch (Exception ex) {
                ViewData["RegistrationError"] = ex.Message;
            }

            


            return View("Register");
        }

        public ActionResult About() {
            return View("About");
        }

        public ActionResult Terms() {
            return View("Terms");
        }
    }
}
